<template>
	<view class="body">
		<view class="item-wapper" @tap="checkupdate">
			<view class="left-wapper">
				<view class="info-img">
					<image src="../../static/icons/icon_version_update.png" mode=""></image>
				</view>
				<view class="info-words">
					版本更新
				</view>
			</view>
			<view class="right-wapper">
				<view class="gray-fields">
					{{version}}
				</view>
				<view class="arrow-block">
					<image src="../../static/icons/left-gray-arrow.png" class="arrow-ico"></image>
				</view>
			</view>
		</view>
		<view class="item-wapper" style="margin-top: 2upx;" @tap="toAboutus">
			<view class="left-wapper">
				<view class="info-img">
					<image src="../../static/icons/icon_aboutus.png" mode=""></image>
				</view>
				<view class="info-words">
					关于我们
				</view>
			</view>
			<view class="right-wapper">
				<view class="gray-fields">
				</view>
				<view class="arrow-block">
					<image src="../../static/icons/left-gray-arrow.png" class="arrow-ico"></image>
				</view>
			</view>
		</view>
		<view class="footer-wapper menu-wrapper">
			<view class="btn-wrapper">				
				<button type="primary" @tap="loginOut">退出登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				version:''
			};
		},
		onShow(){
			var _this=this;
			this.version=_this.$config.SysConfig.version
		},
		methods: {
			checkupdate(){
				var _this=this;
				_this.$common.isWifiExec(()=>{
					uni.showLoading({
					    title: _this.$config.msgs.isChecking
					});
					
					var data={}
					_this.$http.update_v(data).then(res=>{
						if(res.code=='1'){
							console.log(res.data)
							console.log(_this.version)
							if(res.data.version.toString()===_this.version.toString()){
								uni.hideLoading()
								uni.showToast({
									icon:'none',
									title:_this.$config.msgs.isNewest,
									duration:1500
								})
							}
							else{
								uni.hideLoading()
								//下载文件并打开
								var downloadtask=plus.downloader.createDownload(res.data.url,{},function(d,status){
									if(status==200){
										plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){
											uni.showToast({
												title:_this.$config.msgs.install_fail,
												mask:false,
												duration:1500
											})
										})
									} else {
										uni.showToast({
											title:_this.$config.msgs.update_fail,
											mask:false,
											duration:1500
										})
									}
								});
								try{
									downloadtask.start();
									var prg=0;
									var showLoading=plus.nativeUI.showWaiting(_this.$config.msgs.isDownloading);
									downloadtask.addEventListener('statechanged',function(task,status){
										switch(task.state){
											case 1:
											showLoading.setTitle(_this.$config.msgs.isDownloading);
											break;
											case 2:
											showLoading.setTitle(_this.$config.msgs.isConnected);
											break;
											case 3:
											prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
											showLoading.setTitle("  正在下载" + prg + "%  ");
											break;
											case 4:
											plus.nativeUI.closeWaiting();
											break;
										}
									})
								}
								catch(err){
									plus.nativeUI.closeWaiting();
									uni.showToast({
										title:_this.$config.msgs.update_fail,
										mask:false,
										duration:1500
									})
								}
							}
						}
						else{
							uni.hideLoading()							
							uni.showToast({
								icon:'none',
								title:res.msg,
								duration:1500
							})
						}
					});					
				},()=>{
					// uni.showToast(_this.$config.msgs.needwifi);
					uni.showToast({
						icon:'none',
						title:_this.$config.msgs.needwifi,
						duration:1500
					})
				})
			},
			toAboutus(){
				uni.navigateTo({
					url:'../aboutus/aboutus'
				})
			},
			loginOut(){
				uni.showModal({
					title:'提示',
					content:'您确定要退出吗?',
					success:(res)=>{
						if (res.confirm) {
							this.$config.SysConfig.token=''
							this.$config.SysConfig.loginedUser={}
							this.$config.SysConfig.wrhus_id=''
							this.$config.SysConfig.inventlocdef_code=''
							
							uni.redirectTo({
								url:'../login/login'
							})
						}
					}
				})				
			}
		}
	}
</script>

<style>
.body{
	width:100%;
	height: 100%;
	position: absolute;
	background: #D8D8D8;
}
.item-wapper {
	display: flex;
	flex-direction: row;
	background-color: #FFFFFF;
}
.item-wapper .right-wapper,.item-wapper .left-wapper{	
	display: flex;
	flex-direction: row;
}
.item-wapper .left-wapper{
	width: 60%;
	justify-content: flex-start;
}
.item-wapper .right-wapper {
	width: 40%;
	justify-content: flex-end;
}
.info-img image{	
	padding: 20upx 5upx 0upx 30upx;
	width: 50upx;
	height: 50upx;
}

.info-words {
	color: #333333;
	font-size: 16px;
	line-height: 80upx;
	padding: 5upx 5upx 5upx 15upx;
}
.arrow-block {
	margin-left: 10upx;
	line-height: 90upx;
	margin-right: 20upx;
}
.arrow-ico {
	width: 30upx;
	height: 30upx;
}
.gray-fields {
	font-size: 14px;
	color: darkgray;
	line-height: 80upx;
}
/* .footer-wapper {
	position: fixed;
	bottom: 0;
	
	display: flex;
	flex-direction: column;
	width: 92%;
} */
.menu-wrapper{
	flex-direction: row;
	display: flex;
}
.menu-wrapper .btn-wrapper{
	width: 100%;
	padding: 15upx 30upx;
}

</style>
